package com.cuit.dao;
import java.sql.*;

import com.cuit.pojo.DoctorOrder;
import com.cuit.util.JdbcUtils;
public class D_OrderDao {
    public D_OrderDao(){}
    public void uptaM(DoctorOrder order){
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {//JDBC查询
            conn = JdbcUtils.getConnection();
            //循环week，有则更改，无则新添。
            for(int i=0;i<order.getD_week().length;i++){
                String sql = "select * from d_order where d_id=? and d_week=?;"; //数据库编译时
                stmt = conn.prepareStatement(sql);    //将sql发送给数据库进行编
                stmt.setInt(1, order.getD_id());//传入数据值，不会作为关键字 --防止注入
                stmt.setInt(2,order.getD_week()[i]);
                rs=stmt.executeQuery();            //执行sql
                if(rs.next()==true){
                    String sql2 = "update d_order set max_order= ? where d_id= ? and d_week= ?; "; //数据库编译时
                    stmt = conn.prepareStatement(sql2);    //将sql发送给数据库进行编
                    stmt.setInt(1,order.getMax_order());
                    stmt.setInt(2, order.getD_id());//传入数据值，不会作为关键字 --防止注入
                    stmt.setInt(3,order.getD_week()[i]);
                    stmt.executeUpdate();            //执行sql
                }else{
                    String sql2 = "insert into d_order(max_order,d_id,d_week) values (?,?,?);"; //数据库编译时
                    stmt = conn.prepareStatement(sql2);    //将sql发送给数据库进行编
                    stmt.setInt(1,order.getMax_order());
                    stmt.setInt(2, order.getD_id());//传入数据值，不会作为关键字 --防止注入
                    stmt.setInt(3,order.getD_week()[i]);
                    stmt.executeUpdate();            //执行sql
                }
                //一个不会被医生修改的最大预约数
                String sql4 = "select * from d_order where d_id=? and d_week=? and d_sitting_time='早上';";
                stmt = conn.prepareStatement(sql4);    //将sql发送给数据库进行编
                stmt.setInt(1, order.getD_id());//传入数据值，不会作为关键字 --防止注入
                stmt.setInt(2,order.getD_week()[i]);
                rs=stmt.executeQuery();            //执行sql

                if(rs.next()==true){
                    String sql3 = "update d_order set max_order=? where d_id= ? and d_week=? and d_sitting_time='早上';"; //数据库编译时
                    stmt = conn.prepareStatement(sql3);    //将sql发送给数据库进行编
                    stmt.setInt(1,order.getMax_order());
                    stmt.setInt(2, order.getD_id());
                    stmt.setInt(3,order.getD_week()[i]);
                    stmt.executeUpdate();            //执行sql
                }else{
                    String sql3 = "insert into d_order(max_order,d_id,d_sitting_time,d_week) values (?,?,'早上',?); "; //数据库编译时
                    stmt = conn.prepareStatement(sql3);    //将sql发送给数据库进行编
                    stmt.setInt(1,order.getMax_order());
                    stmt.setInt(2, order.getD_id());
                    stmt.setInt(3,order.getD_week()[i]);
                    stmt.executeUpdate();            //执行sql
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void uptaS(DoctorOrder order){
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {//JDBC查询
            conn = JdbcUtils.getConnection();
            //循环，找到每天，每个时间段
            for(int i=0;i<order.getD_week().length;i++){
                for(int j=0;j<order.getD_sitting_time().length;j++){
                    String sql2 = "select * from d_order where d_id=? and d_week=? ;"; //数据库编译时
                    stmt = conn.prepareStatement(sql2);
                    stmt.setInt(1, order.getD_id());
                    stmt.setInt(2,order.getD_week()[i]);
                    rs=stmt.executeQuery();            //执行sql
                    boolean flag=false;
                    String t=null;
                    if(rs.next()==true){
                        flag=true;
                        t=rs.getString("d_sitting_time");
                    }
                    System.out.println("t=============================="+t);
                    //如果没有rs，添加；如果有rs，判断有无t，如果有t判断是不是一样的，如果是一样的则不变，如果不是一样的则添加；如果无t直接更改。
                    if(flag==false||(flag==true&&t!=null)){
                        String sql = "select * from d_order where d_id=? and d_week=? and d_sitting_time=?;"; //数据库编译时
                        stmt = conn.prepareStatement(sql);    //将sql发送给数据库进行编
                        stmt.setInt(1, order.getD_id());//传入数据值，不会作为关键字 --防止注入
                        stmt.setInt(2,order.getD_week()[i]);
                        stmt.setString(3,order.getD_sitting_time()[j]);
                        rs=stmt.executeQuery();            //执行sql
                        //对比sitting——time，如果有了，不添加，如果没有，添加。
                        if(rs.next()!=true){
                            sql = "select * from d_order where d_id=? and d_week=? and d_sitting_time='早上';"; //数据库编译时
                            stmt = conn.prepareStatement(sql);
                            stmt.setInt(1, order.getD_id());
                            stmt.setInt(2,order.getD_week()[i]);
                            rs=stmt.executeQuery();
                            int m=0;
                            if(rs.next()==true){
                                m=rs.getInt("max_order");
                            }
                            String sql5 = "insert into d_order(max_order,d_id,d_sitting_time,d_week) value (?,?,?,?);"; //数据库编译时
                            stmt = conn.prepareStatement(sql5);
                            stmt.setInt(1,m);
                            stmt.setInt(2, order.getD_id());
                            stmt.setString(3,order.getD_sitting_time()[j]);
                            stmt.setInt(4,order.getD_week()[i]);
                            stmt.executeUpdate();
                            }
                    }else{
                        String sql = "update d_order set d_sitting_time=? where d_id=? and d_week=?;";
                        stmt = conn.prepareStatement(sql);
                        stmt.setString(1,order.getD_sitting_time()[j]);
                        stmt.setInt(2, order.getD_id());
                        stmt.setInt(3,order.getD_week()[i]);
                        //传入数据值，不会作为关键字 --防止注入
                        stmt.executeUpdate();            //执行sql
                    }
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void deS(DoctorOrder order){
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {//JDBC查询
            conn = JdbcUtils.getConnection();
            for(int j=0;j<order.getD_week().length;j++){
                for(int i=0;i<order.getD_sitting_time().length;i++){
                    String sql = "delete from d_order where d_sitting_time=? and d_id=? and d_week=?"; //数据库编译时
                    stmt = conn.prepareStatement(sql);
                    stmt.setString(1,order.getD_sitting_time()[i]);
                    stmt.setInt(2, order.getD_id());//传入数据值，不会作为关键字 --防止注入
                    stmt.setInt(3,order.getD_week()[j]);
                    stmt.executeUpdate();            //执行sql
                    System.out.println("删除成功");
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
    public int[] getMax(int id){
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        int maxs[]=new int[7];
        try {//JDBC查询
            conn = JdbcUtils.getConnection();
            for(int j=0;j<7;j++){
                String sql = "select * from d_order where d_id=? and d_week=?;"; //数据库编译时
                stmt = conn.prepareStatement(sql);
                stmt.setInt(1,id);
                stmt.setInt(2, j+1);//传入数据值，不会作为关键字 --防止注入
                rs=stmt.executeQuery();            //执行sql
                if(rs.next()==true){
                    maxs[j]=rs.getInt("max_order");
                }
                //给早上传值
                String sql2="select * from d_order where d_id=? and d_week=? and d_sitting_time='早上';";
                stmt = conn.prepareStatement(sql2);
                stmt.setInt(1,id);
                stmt.setInt(2, j+1);//传入数据值，不会作为关键字 --防止注入
                ResultSet rs2=stmt.executeQuery();            //执行sql
                if(rs2.next()!=true){
                    String sql3="insert into d_order(max_order,d_id,d_sitting_time,d_week) value (?,?,'早上',?);";
                    stmt = conn.prepareStatement(sql3);
                    stmt.setInt(1,maxs[j]);
                    stmt.setInt(2,id);
                    stmt.setInt(3, j+1);//传入数据值，不会作为关键字 --防止注入
                    stmt.executeUpdate();
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return maxs;
    }
    public String[][] getTime(int id){
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        String time[][]=new String[7][3];
        try {//JDBC查询
                conn = JdbcUtils.getConnection();
                for(int j=0;j<7;j++){
                    String sql = "select * from d_order where d_id=? and d_week=? and d_sitting_time=?"; //数据库编译时
                    stmt = conn.prepareStatement(sql);
                    stmt.setInt(1,id);
                    stmt.setInt(2, j+1);
                    stmt.setString(3,"上午" );
                    rs=stmt.executeQuery();            //执行sql
                    if(rs.next()==true){
                        time[j][0]="√";
                    }
                    String sql2 = "select * from d_order where d_id=? and d_week=? and d_sitting_time=?"; //数据库编译时
                    stmt = conn.prepareStatement(sql);
                    stmt.setInt(1,id);
                    stmt.setInt(2, j+1);
                    stmt.setString(3,"下午" );
                    rs=stmt.executeQuery();            //执行sql
                    if(rs.next()==true){
                        time[j][1]="√";
                    }
                    String sql3 = "select * from d_order where d_id=? and d_week=? and d_sitting_time=?"; //数据库编译时
                    stmt = conn.prepareStatement(sql);
                    stmt.setInt(1,id);
                    stmt.setInt(2, j+1);
                    stmt.setString(3,"晚上" );
                    rs=stmt.executeQuery();            //执行sql
                    if(rs.next()==true){
                        time[j][2]="√";
                    }
                }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return time;
    }
}
